package one.one.thirtyFour;

import java.util.Arrays;

public class Matrix {

	static double[] dot(double[] x, double[] y) {
		double[] r = new double[x.length];
		for (int i = 0; i < r.length; i++) {
			r[i] = x[i] * y[i];
		}
		return r;
	}

	static double sum(double[] a) {
		double r = 0;
		for (int i = 0; i < a.length; i++) {
			r += a[i];
		}
		return r;
	}

	static double[] col(double[][] a, int c) {
		double[] r = new double[a.length];
		for (int i = 0; i < r.length; i++) {
			r[i] = a[i][c];
		}
		return r;
	}

	static double[][] mult(double[][] a, double[][] b) {

		double[][] r = new double[a.length][b[0].length];
		double[] pre;
		double[] post;
		for (int i = 0; i < r.length; i++) {
			for (int j = 0; j < r[0].length; j++) {
				pre = a[i];
				post = col(b, j);
				r[i][j] = sum(dot(pre, post));
			}
		}

		return r;
	}

	static double[][] transpose(double[][] a) {
		double[][] r = new double[a[0].length][a.length];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				r[j][i] = a[i][j];
			}
		}
		return r;
	}

	static double[] mult(double[][] a, double[] x) {
		double[] r = new double[a.length];
		for (int i = 0; i < a.length; i++) {
			r[i] = 0;
			for (int j = 0; j < a[i].length; j++) {
				r[i] += a[i][j] * x[j];
			}
		}
		return r;
	}

	static double[] mult(double[] y, double[][] a) {
		double[] r = new double[a[0].length];
		for(int i=0;i<r.length;i++) {
			r[i]=0;
			for(int j=0;j<a.length;j++) {
				r[i]+=y[j]*a[j][i];
			}
		}
		return r;
	}

	public static void main(String[] args) {
		double[][] a = { { 1, 2 }, { 3, 4 } };
		// double[] b = { 1, 2 };
		// double[] r = mult(a, b);

		double[] y = { 1, 2 };
		double[] r = mult(y, a);
		System.out.println(Arrays.toString(r));

	}

	public static void mains(String[] args) {
		double[][] a = { { 2, 1 }, { 4, 3 } };
		double[][] b = { { 1, 2 }, { 1, 0 } };

		double[][] r = mult(a, b);

		for (int i = 0; i < r.length; i++) {
			System.out.println(Arrays.toString(r[i]));
		}
	}

}
